﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CP.Entities;
using CP.Controllers.GPM;
using DevExpress.XtraEditors;
using CP.Utility;

namespace GPM
{
    public partial class FrmUserFunc : BaseForm
    {
        public FrmUserFunc()
        {
            InitializeComponent();
            this.BarComponents = components;
        }

        private void btnResourceQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (editResourceName.EditValue == null)
            {
                ExMessageBox.Show("资源名不能为空", AppSetter.SoftName);
                return;
            }
            try
            {
                UserFuncController uc = new UserFuncController();
                DataTable dt = uc.GetResourceInfo(this.radioType.Properties.Items[
                    this.radioType.SelectedIndex].Value.ToString(),
                    this.editResourceName.EditValue.ToString());

                gcResource.DataSource = dt;
            }
            catch (Exception ex)
            {
                ExMessageBox.Show("提示信息：错误信息[" + ex.Message + "]", AppSetter.SoftName);
            }
        }

        private void FrmUserFunc_Load(object sender, EventArgs e)
        {
            
        }

        private void gvResource_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gvResource.GetFocusedDataRow();
            if (null == dr)
                return;
            int webpartid = Convert.ToInt32(dr["webpart_id"] ?? "0");
            int resourceid = Convert.ToInt32(dr["resource_id"] ?? "0");
            int menuid = Convert.ToInt32(dr["menu_id"] ?? "");
            if (0 == menuid)
            {
                UserGroupController gc = new UserGroupController();
                gcAuthGroup.DataSource = gc.GetGroupByMenuID(resourceid);
                UserFuncController fc = new UserFuncController();
                gcAuthUser.DataSource = fc.GetUsersByMenuID(resourceid);
            }
            else
            {
                UserGroupController gc = new UserGroupController();
                gcAuthGroup.DataSource = gc.GetGroupByButtonID(webpartid, resourceid);
                UserFuncController fc = new UserFuncController();
                gcAuthUser.DataSource = fc.GetUsersByButtonID(webpartid, resourceid);
            }
        }

        private void gvAuthGroup_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            User_Group group = gvAuthGroup.GetFocusedRow() as User_Group;
            if (null == group)
                return;
            UserFuncController fc = new UserFuncController();
            gcGroupUser.DataSource = fc.GetUsersByGroupID(group.group_id);
        }

        private void gvAuthUser_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            User_Info user = gvAuthUser.GetFocusedRow() as User_Info;
            if (null == user)
                return;
            UserGroupController uc = new UserGroupController();
            gcUserGroup.DataSource = uc.GetGroupByUserID(user.user_id);
        }

    }
}